package com.accp.demo.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.accp.demo.dao.CommonDAO;
import com.accp.demo.entity.SysUser;

public class UserService implements IUserService {

	private CommonDAO<SysUser> userDAO;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.userDAO=new CommonDAO<SysUser>(sessionFactory,SysUser.class);
	}
	
	@Override
	public boolean checkUserName(String userName) {
		String hql="from sys_user u where u.usr_name=?";
		List list=this.userDAO.findByHQL(hql, userName);
		return (list.size()>0);
	}

	@Override
	public List findAll() {
		return this.userDAO.findAll();
	}

	@Override
	public SysUser findById(Integer id) {
		// TODO Auto-generated method stub
		return this.userDAO.findById(id);
	}

	@Override
	public void addUser(SysUser user) {
		this.userDAO.save(user);
	}
	

	@Override
	public void delUser(Integer id) {
		this.userDAO.delete(this.userDAO.findById(id));
	}

	@Override
	public void updateUser(SysUser user) {
	
		this.userDAO.update(user);
	}

	@Override
	public SysUser login(SysUser user) {
		String hql="from Userinfo user where user.usr_name =? and user.usr_password=?";
		
		List list=this.userDAO.findByHQL(hql,user.getUsrName(),user.getUsrPassword());
		
		if(list ==null || list.isEmpty()){
			return null;
		}
		
		
		return user;
	}
	
	
	
//	public static void main(String[] args) {
//		BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
//		
//		IUserService us=(IUserService) bf.getBean("userService");
//		
//		SysUser user=us.findById(17);
//		
//	}
	
	
	
	
	
	
	

}
